<html>
<head>
  <title>python奇技淫巧</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="620"/>
<h1>python奇技淫巧</h1>

<div>
<span><div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">格式化输出到txt</span></div><div style="background-color: rgb(64, 64, 64); font-size: 11.3pt;"><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">for </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">word,count </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">in </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">wordSorted</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f1 </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 204, 204); font-family: Consolas;">open</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">&quot;txt/out.txt&quot;</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">, </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">&quot;a&quot;</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">, </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(106, 69, 153); font-family: Consolas;">encoding</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">=</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'UTF-8'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">) </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: Consolas;">#</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: 宋体;">这个模式</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: Consolas;">a</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: 宋体;">很吊</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: 宋体;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">str1 </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'{}: {} </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 255, 0); font-family: Consolas; font-weight: bold;">\n</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">.format</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">word,count</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)           </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: Consolas;">#</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: 宋体;">这个</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: Consolas;">.format</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: 宋体;">也很叼</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 102, 102); font-family: 宋体;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f1.write</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">str1</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f1.close</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">()</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 204, 204); font-family: Consolas;">print</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">word,</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">':'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">,count</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">正则化表达式</span></div><div style="background-color: rgb(64, 64, 64); font-size: 11.3pt;"><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">words </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">re.split</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'[,.\s]\s*'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">,line</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">遍历文件夹</span></div><div style="background-color: rgb(64, 64, 64); font-size: 11.3pt;"><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">def </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">walk_dir_and_resize</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">path</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">for </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">root,dirs,files </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">in </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">os.walk</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">path</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">       </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">for </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f_name </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">in </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">files</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">           </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">if </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f_name.lower</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">()</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">.endswith</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'jpg'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">               </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">path_dst </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">os.path.join</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">root,f_name</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">               </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f_new_name </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'iPhone5_' </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">+ </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f_name</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">                resize_iPhone_pic</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(106, 69, 153); font-family: Consolas;">path</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">=</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">path_dst,</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(106, 69, 153); font-family: Consolas;">new_path</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">=</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f_new_name</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">寻址并替换</span></div><div style="background-color: rgb(64, 64, 64); font-size: 11.3pt;"><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">def </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">trans_to_words</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">()</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">type_in </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 204, 204); font-family: Consolas;">input</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">&quot;&gt;&quot;</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">with </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 204, 204); font-family: Consolas;">open</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'txt/filtered_words.txt'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">,</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(106, 69, 153); font-family: Consolas;">encoding</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">=</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'UTF-8'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">) </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">as </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">       </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">text </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">f.read</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">()</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 204, 204); font-family: Consolas;">print</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">text.split</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">&quot;</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 255, 0); font-family: Consolas; font-weight: bold;">\n</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">&quot;</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">))</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">for </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">i </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">in </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">text.split</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">&quot;</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 255, 0); font-family: Consolas; font-weight: bold;">\n</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">&quot;</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">       </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">if </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">i </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(204, 153, 0); font-family: Consolas; font-weight: bold;">in </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">type_in</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">:     ***非常强大，既可遍历数组，列表等***</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">           </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">type_in </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(255, 255, 255); font-family: Consolas; font-weight: bold;">= </span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">type_in.</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(173, 0, 0); font-family: Consolas;">replace</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">i,</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 181, 128); font-family: Consolas; font-weight: bold;">'**'</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div><div><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">   </span> <span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(0, 204, 204); font-family: Consolas;">print</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">(</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas;">type_in</span><span style="background-color: rgb(64, 64, 64); font-size: 11.3pt; color: rgb(155, 194, 142); font-family: Consolas; font-weight: bold;">)</span></div></div><div><br/></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(28, 51, 135);">将ASCLL码值转换为字符 </span></font></div><div style="margin-top: 1em; margin-bottom: 1em;">示例：</div><div>ord('a')表示a在ASSCLL码中的序号，为97；</div><div><br/></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">map的使用</span></div><div><ul><li><span style="line-height: 1.45;">对可迭代函数'iterable'中的每一个元素应用‘function’方法，将结果作为list返回。</span></li></ul><div>          </div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; def add100(x):</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">...     return x+100</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">... </span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; hh = [11,22,33]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; map(add100,hh)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">[111, 122, 133]</span></div><div><br/></div></div><div><br/></div><div>就像文档中说的：<font style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">对hh中的元素做了add100，返回了结果的list。</span></font></div><div>        </div><ul><li>如果给出了额外的可迭代参数，则对每个可迭代参数中的元素‘<span style="background-color: rgb(229, 51, 51);">并行</span>’的应用‘function’。（翻译的不好，这里的关键是‘并行’）</li></ul><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; def abc(a, b, c):</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">...     return a*10000 + b*100 + c</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">...</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; list1 = [11,22,33]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; list2 = [44,55,66]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; list3 = [77,88,99]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; map(abc,list1,list2,list3)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">[114477, 225588, 336699]</span></div><div><br/></div></div><div>看到并行的效果了吧！<font style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">在每个list中，取出了下标相同的元素</span></font>，执行了abc()。</div><div><br/></div><ul><li>如果'function'给出的是‘None’，自动假定一个‘identity’函数（这个‘identity’不知道怎么解释，看例子吧<img src="python奇技淫巧_files/Image.gif" type="image/gif" data-filename="Image.gif"/>）</li></ul></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; list1 = [11,22,33]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; map(None,list1)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">[11, 22, 33]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; list1 = [11,22,33]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; list2 = [44,55,66]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; list3 = [77,88,99]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt; map(None,list1,list2,list3)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">[(11, 44, 77), (22, 55, 88), (33, 66, 99)]</span></div><div><br/></div></div><div><font style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">取下标相同的元素构成元组</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><h1><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: normal;">enumerate() 函数</span></h1><div><span style="font-size: 11pt;">enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。</span></div><div><br/></div><ul><li>如果对一个列表，既要遍历索引又要遍历元素时，首先可以这样写：</li></ul><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">list1 = [&quot;这&quot;, &quot;是&quot;, &quot;一个&quot;, &quot;测试&quot;]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">for i in range (len(list1)):</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print i ,list1[i]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">* 1</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">* 2</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">* 3</span></div><div><br/></div></div><ul><li>上述方法有些累赘，利用enumerate()会更加直接和优美：</li></ul><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">list1 = [&quot;这&quot;, &quot;是&quot;, &quot;一个&quot;, &quot;测试&quot;]</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">for index, item in enumerate(list1):</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    print index, item</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt;&gt;&gt;</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">0 这</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">1 是</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">2 一个</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">3 测试</span></div><div><br/></div></div><div><br/></div></div><div><br/></div><div><font color="#1C3387" style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">回调函数（callback）</span></font></div><div><br/></div><div>        你到一个商店买东西，刚好你要的东西没有货，于是你在店员那里留下了你的电话，过了几天店里有货了，店员就打了你的电话，然后你接到电话后就到店里去取了货。在这个例子里，你的电话号码就叫回调函数，你把电话留给店员就叫登记回调函数，店里后来有货了叫做触发了回调关联的事件，店员给你打电话叫做调用回调函数，你到店里去取货叫做响应回调事件。回答完毕。</div><div><a href="https://www.zhihu.com/question/19801131">https://www.zhihu.com/question/19801131</a></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font color="#1C3387" style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">“阻塞”与&quot;非阻塞&quot;与&quot;同步&quot;与“异步&quot;</span></font></div><div><br/></div><div><span style="font-weight: bold;">1.同步与异步</span></div><div>同步和异步关注的是<span style="font-weight: bold;">消息通信机制</span> (synchronous communication/ asynchronous communication)</div><div>所谓同步，就是在发出一个*调用*时，在没有得到结果之前，该*调用*就不返回。但是一旦调用返回，就得到返回值了。</div><div style="margin-top: 1em; margin-bottom: 1em;">换句话说，就是由*调用者*主动等待这个*调用*的结果。</div><div style="margin-top: 1em; margin-bottom: 1em;">而异步则是相反，<span style="font-weight: bold;">*调用*在发出之后</span><span style="font-weight: bold;">，这个调用就直接返回了，所以没有返回结果</span>。换句话说，当一个异步过程调用发出后，调用者不会立刻得到结果。而是在*调用*发出后，*被调用者*通过状态、通知来通知调用者，或通过回调函数处理这个调用。</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">举个通俗的例子：</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">你打电话问书店老板有没有《分布式系统》这本书，如果是同步通信机制，书店老板会说，你稍等，”我查一下&quot;，然后开始查啊查，等查好了（可能是5秒，也可能是一天）告诉你结果（返回结果）。</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">而异步通信机制，书店老板直接告诉你我查一下啊，查好了打电话给你，然后直接挂电话了（不返回结果）。然后查好了，他会主动打电话给你。在这里老板通过“回电”这种方式来回调。</span></div><div><br/></div></div><div><br/></div><div><br/></div><div>2. 阻塞与非阻塞</div><div style="margin-top: 1em; margin-bottom: 1em;">阻塞和非阻塞关注的是<font style="font-size: 12pt;"><span style="color: rgb(50, 135, 18); font-size: 12pt; font-weight: bold;">程序在等待调用结果（消息，返回值）时</span></font><span style="color: rgb(50, 135, 18); font-size: 12pt; font-weight: bold;">的状态</span><span style="font-weight: bold;">.</span></div><div>阻塞调用是指调用结果返回之前，当前线程会被挂起。调用线程只有在得到结果之后才会返回。</div><div style="margin-top: 1em; margin-bottom: 1em;">非阻塞调用指在不能立刻得到结果之前，该调用不会阻塞当前线程。</div><div>还是上面的例子，</div><div>你打电话问书店老板有没有《分布式系统》这本书，你如果是阻塞式调用，你会一直把自己“挂起”，直到得到这本书有没有的结果，如果是非阻塞式调用，你不管老板有没有告诉你，你自己先一边去玩了， 当然你也要偶尔<span style="font-weight: bold; font-size: 12pt;">过几分钟check一下</span>老板有没有返回结果。</div><div style="margin-top: 1em; margin-bottom: 1em;"><font color="#AD0000" style="font-size: 14pt;"><span style="color: rgb(173, 0, 0); font-size: 14pt; font-weight: bold;">在这里阻塞与非阻塞与是否同步异步无关。跟老板通过什么方式回答你结果无关。</span></font></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>生成器对象</b></font></div><div><span style="-en-paragraph: true;">生成器对象是一个迭代器。但是它比迭代器对象多了一些方法，它们包括send方法，throw方法和close方法。这些方法，主要是用于外部与生成器对象的交互。本文先介绍send方法。</span></div><div><span style="line-height: 1.45;"><br/></span></div><div><span style="line-height: 1.45;">send方法有一个参数，该参数指定的是</span><span style="line-height: 1.45; font-weight: bold;">上一次被挂起的yield语句的返回值</span><span style="line-height: 1.45;">。这样说起来比较抽象，看下面的例子。</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>def MyGenerator():</div><div><span>    value = (yield 1)</span><br/></div><div><span><span>    value = (yield value)</span><br/></span></div><div><span><br/></span></div><div><span>gen = MyGenerator()</span></div><div><span>print(gen.next())</span></div><div><span>print(gen.send(2))</span></div><div><span>print(gen.send(3))</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></span>
</div></body></html> 